package cc.admin.modules.wea.controller;

import cc.admin.common.api.vo.Result;
import cc.admin.common.aspect.annotation.AutoLog;
import cc.admin.common.sys.base.controller.BaseController;
import cc.admin.common.sys.query.QueryGenerator;
import cc.admin.modules.wea.entity.WarnInfo;
import cc.admin.modules.wea.entity.WeaPlatform;
import cc.admin.modules.wea.service.IWeaPlatformService;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 平台基本信息表
 * @Author: cc-admin
 * @Date:   2024-09-03
 * @Version: V1.0.0
 */
@Slf4j
@Api(tags="平台基本信息表")
@RestController
@RequestMapping("/wea/platform")
public class WeaPlatformController extends BaseController<WeaPlatform, IWeaPlatformService> {
	@Autowired
	private IWeaPlatformService weaPlatformService;

	/**
	 * 分页列表查询
	 *
	 * @param key
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "平台基本信息表-分页列表查询")
	@ApiOperation(value="平台基本信息表-分页列表查询", notes="平台基本信息表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(
								   @RequestParam(name="key",required = false) String key,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<WeaPlatform> queryWrapper = QueryGenerator.initQueryWrapper(new WeaPlatform(), req.getParameterMap());
		if (StrUtil.isNotEmpty(key)) {
			queryWrapper.and(i->i.like("id", key).or().like("name", key));
		}

		Page<WeaPlatform> page = new Page<WeaPlatform>(pageNo, pageSize);
		IPage<WeaPlatform> pageList = weaPlatformService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	/**
	 * 添加
	 * @param weaPlatform
	 * @return
	 */
	@AutoLog(value = "平台基本信息表-添加")
	@ApiOperation(value="平台基本信息表-添加", notes="平台基本信息表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody WeaPlatform weaPlatform) {
        weaPlatformService.save(weaPlatform);
		return Result.ok("添加成功！");
	}

	/**
	 * 编辑
	 *
	 * @param weaPlatform
	 * @return
	 */
	@AutoLog(value = "平台基本信息表-编辑")
	@ApiOperation(value="平台基本信息表-编辑", notes="平台基本信息表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody WeaPlatform weaPlatform) {
        weaPlatformService.updateById(weaPlatform);
		return Result.ok("编辑成功!");
	}


	/**
	 *   复制
	 * @param id
	 * @param name
	 * @return
	 */
	@AutoLog(value = "平台基本信息表-复制")
	@ApiOperation(value="平台基本信息表-复制", notes="平台基本信息表-复制")
	@PostMapping(value = "/copy")
	public Result<?> copy(@RequestParam(name="id",required=true) String id,@RequestParam(name="name",required=true) String name) {
        WeaPlatform weaPlatform = weaPlatformService.getById(id);
        weaPlatform.setId(IdUtil.fastUUID());
        weaPlatform.setName(name);
        weaPlatformService.save(weaPlatform);
		return Result.ok("复制成功！");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "平台基本信息表-通过id删除")
	@ApiOperation(value="平台基本信息表-通过id删除", notes="平台基本信息表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        weaPlatformService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "平台基本信息表-批量删除")
	@ApiOperation(value="平台基本信息表-批量删除", notes="平台基本信息表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.weaPlatformService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "平台基本信息表-通过id查询")
	@ApiOperation(value="平台基本信息表-通过id查询", notes="平台基本信息表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        WeaPlatform weaPlatform = weaPlatformService.getById(id);
		return Result.ok(weaPlatform);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param weaPlatform
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, WeaPlatform weaPlatform) {
      return super.exportXls(request, weaPlatform, WeaPlatform.class, "平台基本信息表");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, WeaPlatform.class);
  }

	/**
	 * 获取平台天气警报
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/getWarning", method = RequestMethod.POST)
  public Result<?> getWarning (@RequestParam(name="id",required=true) String id){
	  return Result.ok(weaPlatformService.getWarnInfoList(id));
  }

	/**
	 * 获取七天预报
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/getWeather", method = RequestMethod.POST)
  public Result<?> getWeather(@RequestParam(name="id",required=true) String id){
	  return Result.ok(weaPlatformService.getSevenForecast(id));
  }


	/**
	 * 获取七天天气预警
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/getSevenDayWeather")
	public Result<?> getSevenDayWeather(@RequestParam(name="id",required=true) String id){
		return Result.ok(weaPlatformService.getSevenDayWeather(id));
	}


	@GetMapping(value = "/getSevenDayWeatherWarn")
	public Result<?> getSevenDayWeatherWarn(@RequestParam(name="id",required=true) String id){
		return Result.ok(weaPlatformService.getSevenDayWeatherWarn(id));
	}



}
